//
//  RHChoiceMultipleDeviceVC.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/12/31.
//

#import "RHChoiceMultipleDeviceVC.h"
#import "RHWindowTipManage.h"
#import "UIView+Extension.h"
#import "RHEndPointModel.h"
#import "RHChoiceMultipleDeviceCell.h"
#import "SearchBarView.h"

@interface RHChoiceMultipleDeviceVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) SearchBarView *searhBar;

@property (nonatomic ,strong) UITextField *textField;

@property (nonatomic ,strong) UIView *bgView;

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *searchList;

@property (nonatomic ,strong) NSArray *dataArray;//tableview数据源

@property (nonatomic ,strong) NSMutableArray *selectArr;

@property (nonatomic ,strong) QMUILabel *countLab;
@end

@implementation RHChoiceMultipleDeviceVC

- (NSMutableArray *)searchList{
    if(!_searchList){
        _searchList = [NSMutableArray array];
    }
    return _searchList;
}
- (NSMutableArray *)selectArr{
    if(!_selectArr){
        _selectArr = [NSMutableArray array];
    }
    return _selectArr;
}
- (NSArray *)dataArray{
    if(!_dataArray){
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI{
    
    [self.view setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0]];
    kWeakSelf;
    UIView *touchView = [[UIView alloc] init];
    [touchView setBackgroundColor:UIColor.clearColor];
    [self.view addSubview:touchView];
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, kUI_WINDOW_H, kUI_WINDOW_W, (kUI_WINDOW_H*0.6+kTabbarSafeBottomMargin))];
    [_bgView setBackgroundColor:UIColor.whiteColor];
    [self.view addSubview:_bgView];
    
    QMUIButton *sureBtn = [QMUIButton buttonWithTitle:@"确定".rh_localized titleColor:kCOLOR_HEX(@"#3DBB91") titleFont:kSYSTEMFONT(14)];
    [sureBtn.titleLabel sizeToFit];
    [_bgView addSubview:sureBtn];
    [sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(30);
        make.right.mas_offset(-15);
        make.top.mas_offset(15);
        make.width.offset(sureBtn.titleLabel.width+10);
    }];
   
    _countLab  = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentLeft];
    
    [_bgView addSubview:_countLab];
    [_countLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(kSafe_bottom>0?-kSafe_bottom:-15);
    }];
    
    QMUILabel *titleLab = [QMUILabel labelWithFont:kSYSTEMFONT(16) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentCenter];
    [titleLab setText:@"请选择设备".rh_localized];
    [_bgView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.right.equalTo(sureBtn.mas_left).offset(-3);
        make.centerY.equalTo(sureBtn);
    }];

    
    
    UIView *lineV = [[UIView alloc] init];
    [lineV setBackgroundColor:FontColor(@"#eeeeee")];
    [_bgView addSubview:lineV];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [_tableView setSeparatorColor:FontColor(@"#EBEEF4")];
    [_tableView setBackgroundColor:UIColor.whiteColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    _tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
    [_tableView registerClass:[RHChoiceMultipleDeviceCell class] forCellReuseIdentifier:@"RHChoiceMultipleDeviceCell"];
    [_bgView addSubview:_tableView];
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUI_WINDOW_W,kTabbarSafeBottomMargin+10)];
    [_tableView setTableFooterView:footView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    touchView.userInteractionEnabled = YES;
    [touchView addGestureRecognizer:tap];
    
    [touchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    
    
    [_searhBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.mas_offset(7);
        make.height.mas_equalTo(35);
    }];
    
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.height.mas_equalTo(1);
        make.top.equalTo(titleLab.mas_bottom).offset(15);
    }];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(lineV.mas_bottom).offset(0);
        make.bottom.equalTo(weakSelf.countLab.mas_top).offset(-5);
    }];
    
    [_bgView round:10 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
    
 
    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        weakSelf.choiceResultBlock(nil);
        [weakSelf hidden];
    }];
    
    
    self.tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"nodata_icon_1" titleStr:@"没有数据".rh_localized detailStr:@"".rh_localized];
    
    [self updateCountLab];
}

- (void)setDeviceList:(NSArray<RHDeviceModel *> *)deviceList{
    _deviceList = deviceList;
    _dataArray = _deviceList;
    [self.tableView reloadData];
}

- (void)setCurDevices:(NSArray *)curDevices{
    _curDevices = curDevices;
    self.selectArr  = curDevices.mutableCopy;
    for (RHDeviceModel *model in self.dataArray) {
        if ([self.selectArr containsObject:model.deviceId]) {
            model.isSelected = YES;
        }else{
            model.isSelected = NO;
        }
    }
    [self.tableView reloadData];
}

- (void)hidden{
    [UIView animateWithDuration:0.5 animations:^{
        [self->_bgView setFrame:CGRectMake(0,kUI_WINDOW_H, kUI_WINDOW_W, kUI_WINDOW_H*0.65+kTabbarSafeBottomMargin)];
        [self.view setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0]];
    } completion:^(BOOL finished) {
        [self->_searhBar reset];
        self->_dataArray = self->_deviceList;
        [self.tableView reloadData];
        [[RHWindowTipManage shareWindowTipManage] hidden];
    }];
}


- (void)showAnimation{
    [[PrivateMange share].currentUser userName];
    [UIView animateWithDuration:0.5 animations:^{
        [self->_bgView setFrame:CGRectMake(0,kUI_WINDOW_H-(kUI_WINDOW_H*0.6+kTabbarSafeBottomMargin), kUI_WINDOW_W, kUI_WINDOW_H*0.6+kTabbarSafeBottomMargin)];
        [self.view setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0.3]];
    }];
}



-(void)updateCountLab
{
    _countLab.text = [NSString stringWithFormat:@"已选:%d个".rh_localized,self.selectArr.count];
}

-(void)sureAction
{
    if (self.choiceResultBlock) {
        self.choiceResultBlock(self.selectArr);
    }
    [self hidden];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray?_dataArray.count:0;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 45;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RHChoiceMultipleDeviceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHChoiceMultipleDeviceCell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    if (self.deviceList.count>indexPath.row) {
        RHDeviceModel *model = self.dataArray[indexPath.row];
        [cell setDeviceModel:model];
       
        
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RHDeviceModel *model = self.dataArray[indexPath.row];
    model.isSelected = !model.isSelected;
    if (model.isSelected) {
        if (![self.selectArr containsObject:model.deviceId]) {
            [self.selectArr addObject:model.deviceId];
        }
    }else{
        [self.selectArr removeObject:model.deviceId];
    }
    [tableView reloadData];
    [self updateCountLab];
}

-(void)dealloc{
    
}

@end
